/*

Stickman Adventure - a game created by Daco & his cousin

Copyright (C) 2011-2012  DacoTaco & Wouterke

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation version 2.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.


*/
#include "SDL.h"
#include "SDL_image.h"
#include "SDL_rotozoom.h"
#include "Global.h"
#include "extern.h"

bool _log = false;
SDL_Surface* SPG_CopySurface(SDL_Surface* src)
{
	if(src == NULL)
		return NULL;
    return SDL_ConvertSurface(src, src->format, SDL_SWSURFACE);
}
SDL_Surface *load_image( std::string filename, unsigned int color_key ) 
{ 
	//Temporary storage for the image that's loaded 
	SDL_Surface* loadedImage = NULL; 
	//The optimized image that will be used 
	SDL_Surface* optimizedImage = NULL;
	//Load the image 
	loadedImage = IMG_Load( filename.c_str());
	//If nothing went wrong in loading the image 
	if( loadedImage != NULL ) 
	{ 
		//Create an optimized image 
		optimizedImage = SDL_DisplayFormat( loadedImage ); 
		//Free the old image 
		if(optimizedImage != NULL)
		{
			//Map the color key 
			if(color_key != 0x0)
			{

				//gprintf("0x%X - 0x%X - 0x%X \n", (color_key << 8) >> 24 , (color_key << 16) >> 24, (color_key << 24) >> 24);
				Uint32 colorkey = SDL_MapRGBA( optimizedImage->format, (color_key << 8) >> 24, (color_key << 16) >> 24, (color_key << 24) >> 24,1 );
				SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey );
			}
			SDL_FreeSurface( loadedImage ); 
			//Return the optimized image 
			return optimizedImage;
		}

	}
	return loadedImage;
}
#ifdef OGL_MODE
void apply_surface( int x, int y, int h,int w, GLuint *texture, SDL_Rect* clip)
{
	if(texture == 0 || *texture == 0)
		return;
	glBindTexture(GL_TEXTURE_2D, *texture);

	glPushMatrix();
	glTranslatef((GLfloat)x,(GLfloat)y,0);

	if(clip != NULL)
	{
		GLfloat clip_w = 0;
		GLfloat clip_h = 0;
		GLfloat clip_x = 0;
		GLfloat clip_y = 0;
		if(clip->h != 0)
		{
			clip_h = 1 / ((GLfloat)h / (GLfloat)clip->h);
			if(clip->y != 0)
				clip_y = (1 * clip_h) * ((GLfloat)clip->y / (GLfloat)clip->h);
		}
		if(clip->w != 0)
		{
			clip_w = 1 / ((GLfloat)w / (GLfloat)clip->w);
			if(clip->x != 0)
				clip_x = (1 * clip_w) * ((GLfloat)clip->x / (GLfloat)clip->w);
		}
		//printf("OW YE BTW : sh : %d sw : %d || ch %d cw %d cx %d cy %d || c_w %f c_h %f || c_x %f c_y %f\n",h,w,clip->h,clip->w,clip->x,clip->y,clip_w,clip_h,clip_x,clip_y);
		glBegin(GL_QUADS);
			//glColor3f(1,1,1);
			glTexCoord2f(clip_x,clip_y);
			glVertex3i(0, 0, 0);

			glTexCoord2f(clip_w+clip_x, clip_y);
			glVertex3i(clip->w, 0, 0);

			glTexCoord2f(clip_w+clip_x,clip_y+clip_h);
			glVertex3i(clip->w, clip->h, 0);

			glTexCoord2f(clip_x,clip_y+clip_h);
			glVertex3i(0, clip->h, 0);
		glEnd();
	}
	else
	{
		//printf("NOPE.AVI : sh : %d sw : %d -> x : %d & y: %d\n",h,w,x,y);
		glBegin(GL_QUADS);
			glTexCoord2i( 0, 0 );
			glVertex3f(0, 0, 0);
			glTexCoord2i(1, 0);
			glVertex3f((GLfloat)w, 0, 0);
			glTexCoord2i(1, 1);
			glVertex3f((GLfloat)w, (GLfloat)h, 0);
			glTexCoord2i(0, 1);
			glVertex3f(0, (GLfloat)h, 0);
		glEnd();
	}
	
	glPopMatrix();
	glLoadIdentity();
	return;
}
#endif

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip)
{ 
#ifndef OGL_MODE
	if(source == NULL || destination == NULL)
		return;
	//Make a temporary rectangle to hold the offsets 
	SDL_Rect offset; 
	//Give the offsets to the rectangle 
	offset.x = x; 
	offset.y = y;
	//Blit the surface 
	SDL_BlitSurface( source, clip, destination, &offset );
#else
	printf("warning : a SDL apply surface was called in OGL mode\n");
#endif
	return;
}
void apply_surface( SDL_Rect* colrect, SDL_Surface* destination, int colour)
{ 
	if(colrect == NULL)
		return;
#ifndef OGL_MODE
	SDL_FillRect( destination, colrect, SDL_MapRGB( destination->format, (colour & 0xFFFFFF) >> 16,(colour & 0xFFFF) >> 8, colour & 0xFF ) );
#else
	glTranslatef( colrect->x, colrect->y, 0 );

    //Start quad
    glBegin( GL_QUADS );

        //Set color to white
        glColor4f( 1, 1, 1, 1.0 );

        //Draw square
	    glVertex3f( 0,            0,             0 );
	    glVertex3f( colrect->w, 0,             0 );
	    glVertex3f( colrect->w, colrect->h, 0 );
	    glVertex3f( 0,            colrect->h, 0 );

    //End quad
    glEnd();

    //Reset
    glLoadIdentity();
#endif
}
